#include "jbleditor.h"
#include "jdbmodule.h"
#include "strhelper.h"
#include "wxhelper.h"
#include "jglobals.h"
#include "jappconfig.h"
#include "jinpatient.h"
#include <wx/app.h>
#include "jmainframe.h"
#include "jryjleditor.h"
#include "wxhelper.h"
#include <sstream>

JBLEditor::wswmap_t JBLEditor::s_wswmap;

JBLEditor *JBLEditor::fromLSH(const std::wstring &lsh)
{
    auto it=s_wswmap.find(lsh);
    if(it!=s_wswmap.end())
    {
        return it->second;
    }else
    {
        return nullptr;
    }
}

bool JBLEditor::getBRXX(const std::wstring &key, std::wstring &value) const
{
    auto cit=this->m_brxx.find(key);
    if(cit!=this->m_brxx.end())
    {
        value=cit->second;
        return true;
    }else {
        value=L"";
        return false;
    }
}

std::wstring  JBLEditor::getJSCodeBRXX() const
{
    std::wostringstream sso(std::ios::binary);
    sso<<L"window.病人={";
    for(auto cit : m_brxx)
    {
        sso<<L"\""<<cit.first<<L"\":\""<<cit.second<<L"\",";
    }
    sso<<L"};";
    return sso.str();
}

JBLEditor::JBLEditor(wxWindow *parent,const std::wstring & lsh)
    :JBLEditorUI(parent),m_lsh(lsh)
{
    s_wswmap[m_lsh]=this;

    static const wchar_t * s_bls[]={
        L"入院记录",
        L"24小时入出院记录",
        L"日间病房入出院记录",
        L"病程记录",
        L"产后病程记录",
        L"术后病程记录",
        L"新生儿病程记录",
        L"出院记录",
        L"术前小结",
        L"术前讨论",
        L"手术同意书",
        L"手术记录",
        L"日间手术记录",
        L"知情谈话记录",
        L"会诊单",
        L"死亡记录",
        L"死亡病历讨论",
        L"长期医嘱单",
        L"临时医嘱单",
        nullptr
    };


    wxTreeItemId root=this->m_treeCtrl->AddRoot(L"病历");
    for(int i=0;s_bls[i];++i)
    {
        wxTreeItemId nt=this->m_treeCtrl->AppendItem(root,s_bls[i]);

    }
    wxApp::GetInstance()->CallAfter([this]{
        this->OnUpdateTree();
    });
}

JBLEditor::~JBLEditor()
{
    s_wswmap.erase(m_lsh);
}

void JBLEditor::OnBtnUpdate(wxCommandEvent &event)
{

}

void JBLEditor::OnBtnTemplate(wxCommandEvent &event)
{

}

void JBLEditor::OnTreeItemActivatedBL(wxTreeEvent &event)
{
    event.Skip(true);
    auto item=event.GetItem();
    auto nm=this->m_treeCtrl->GetItemText(item).ToStdWstring();
    wxWindow* panel=new JRYJLEditor(m_notebook,m_lsh);
    m_notebook->AddPage(panel,nm,true);


}



void JBLEditor::OnUpdateTree()
{
    LoginInfoPtr li=JGlobals::GetValue<LoginInfoPtr>(GV_LOGIN_INFO);
    this->m_tcCurYS->SetValue(li->RYXM);
    this->m_tcCurYS->SetClientObject(TClientData<std::wstring>::New(li->RYBH));

    ado::ConnectionPtr con=ado::newHisCon();
static const wchar_t* s_kmap[]={
    L"LSH",L"主键"
    ,L"SFLX",L"收费类型"
    ,L"YLZH",L"医疗症号"
    ,L"BLH",L"住院号"
    ,L"ZYCS",L"住院次数"
    ,L"BRXM",L"姓名"
    ,L"BRXB",L"性别"
    ,L"CSRQ",L"出生日期"
    ,L"BRNL",L"年龄"
    ,L"SFZH",L"身份证号"
    ,L"MZ",L"民族"
    ,L"MZDM",L"民族代码"
    ,L"HYZK",L"婚姻"
    ,L"CSDD",L"出生地"
    ,L"JG",L"籍贯"
    ,L"JGDM",L"籍贯代码"
    ,L"BRDH",L"电话"
    ,L"JTZZ",L"家族住址"
    ,L"ZY",L"职业"
    ,L"ZYDM",L"职业代码"
    ,L"GZDW",L"工作单位"
    ,L"DWDH",L"单位电话"
    ,L"LXR",L"联系人"
    ,L"LXRGX",L"联系人关系"
    ,L"LXRGXDM",L"联系人关系代码"
    ,L"LXRDH",L"联系人电话"
    ,L"LXRDZ",L"联系人地址"
    ,L"RYFS",L"入院方式"
    ,L"RYLX",L"入院类型"
    ,L"RYKS",L"入院科室"
    ,L"RYKSDM",L"入院科室代码"
    ,L"MJZD",L"门急诊断"
    ,L"MZJDDM",L"门急诊断代码"
    ,L"MJZD1",L"门急诊断1"
    ,L"MJZD1DM",L"门急诊断1代码"
    ,L"MJZD2",L"门急诊断2"
    ,L"MJZD2DM",L"门急诊断2代码"
    ,L"MJZD3",L"门急诊断3"
    ,L"MJZD3DM",L"门急诊断3代码"
    ,L"RYSJ",L"入院时间"
    ,L"RYZT",L"入院状态"
    ,L"SZYS",L"收住医师"
    ,L"SZYSDM",L"收住医师代码"
    ,L"DQKS",L"当前科室"
    ,L"DQKSDM",L"当前科室代码"
    ,L"CWYS",L"床位医师"
    ,L"CWYSDM",L"床位医师代码"
    ,L"ZYCWH",L"住院床位号"
    ,L"CWH",L"床位号"
    ,L"SFBF",L"SFBF"
    ,L"YJZE",L"预交总额"
    ,L"XESD",L"限额设定"
    ,L"XEDB",L"XEDB"
    ,L"FYHJ",L"费用合计"
    ,L"CBZD",L"初步诊断"
    ,L"CBZDDM",L"初步诊断代码"
    ,L"CYZD",L"出院诊断"
    ,L"CYZDDM",L"出院诊断代码"
    ,L"CYZT",L"出院状态"
    ,L"CYRQ",L"出院日期"
    ,L"DJR",L"登记人"
    ,L"DJRDM",L"登记人代码"
    ,L"DJRQ",L"登记日期"
    ,L"XGR",L"修改人"
    ,L"XGRDM",L"修改人代码"
    ,L"XGRQ",L"修改日期"
    ,L"RKSJ",L"入科时间"
    ,L"BRNLDW",L"年龄单位"
    ,L"TBNAME",L"数据表名"
,NULL,NULL};


    if(!con)
        return ;
    static const wchar_t sql_t[]=
LR"(
select [LSH], [SFLX], [YLZH], [BLH], [ZYCS], [BRXM], [BRXB], convert(varchar(19),[CSRQ],121) AS CSRQ,
    [BRNL], [SFZH], [MZ], [MZDM], [HYZK], [CSDD], [JG], [JGDM], [BRDH],
    [JTZZ], [ZY], [ZYDM], [GZDW], [DWDH], [LXR], [LXRGX], [LXRGXDM],
    [LXRDH], [LXRDZ], [RYFS], [RYLX],[RYKS], [RYKSDM], [MJZD],
    [MZJDDM], [MJZD1], [MJZD1DM], [MJZD2], [MJZD2DM], [MJZD3],
    [MJZD3DM], convert(varchar(19),[RYSJ],121) AS RYSJ, [RYZT], [SZYS], [SZYSDM], [DQKS], [DQKSDM],
    [CWYS], [CWYSDM], [ZYCWH], [CWH], [SFBF], [YJZE], [XESD],
    [XEDB], [FYHJ], [CBZD], [CBZDDM], [CYZD], [CYZDDM], [CYZT],
    convert(varchar(19),[CYRQ],121) AS CYRQ,
    [DJR], [DJRDM], convert(varchar(19),[DJRQ],121) AS DJRQ, [XGR], [XGRDM], convert(varchar(19),[XGRQ],121) AS XGRQ,
    convert(varchar(19),[RKSJ],121) AS RKSJ, isnull([BRNLDW],'岁') AS BRNLDW,[TBNAME]
from
    (SELECT [LSH], [SFLX], [YLZH], [BLH], [ZYCS], [BRXM], [BRXB], [CSRQ],
     [BRNL], [SFZH], [MZ], [MZDM], [HYZK], [CSDD], [JG], [JGDM], [BRDH],
     [JTZZ], [ZY], [ZYDM], [GZDW], [DWDH], [LXR], [LXRGX], [LXRGXDM],
     [LXRDH], [LXRDZ], [RYFS], [RYLX], [RYKS], [RYKSDM], [MJZD],
     [MZJDDM], [MJZD1], [MJZD1DM], [MJZD2], [MJZD2DM], [MJZD3],
     [MJZD3DM], [RYSJ], [RYZT], [SZYS], [SZYSDM], [DQKS], [DQKSDM],
     [CWYS], [CWYSDM], [ZYCWH], [CWH], [SFBF], [YJZE], [XESD],
     [XEDB], [FYHJ], [CBZD], [CBZDDM], [CYZD], [CYZDDM], [CYZT],
     [CYRQ], [DJR], [DJRDM], [DJRQ], [XGR], [XGRDM], [XGRQ],
     [RKSJ], [BRNLDW],'ZYDJ_BRZL' AS [TBNAME]
    FROM [HIS_330400].[dbo].[ZYDJ_BRZL]
    UNION
    SELECT
    [LSH], [SFLX], [YLZH], [BLH], [ZYCS], [BRXM], [BRXB], [CSRQ],
     [BRNL], [SFZH], [MZ], [MZDM], [HYZK], [CSDD], [JG], [JGDM], [BRDH],
     [JTZZ], [ZY], [ZYDM], [GZDW], [DWDH], [LXR], [LXRGX], [LXRGXDM],
     [LXRDH], [LXRDZ], [RYFS], [RYLX], [RYKS], [RYKSDM], [MJZD],
     [MZJDDM], [MJZD1], [MJZD1DM], [MJZD2], [MJZD2DM], [MJZD3],
     [MJZD3DM], [RYSJ], [RYZT], [SZYS], [SZYSDM], [DQKS], [DQKSDM],
     [CWYS], [CWYSDM], [ZYCWH], [CWH], [SFBF], [YJZE], [XESD],
     [XEDB], [FYHJ], [CBZD], [CBZDDM], [CYZD], [CYZDDM], [CYZT],
     [CYRQ], [DJR], [DJRDM], [DJRQ], [XGR], [XGRDM], [XGRQ],
     [RKSJ], [BRNLDW],'ZYDJ_BRZLLS' AS [TBNAME]
    FROM [HIS_330400].[dbo].[ZYDJ_BRZLLS])
     AS BRZL
WHERE [LSH]='%s'
    )";

    std::wstring sql=str_format(sql_t,this->m_lsh.data());
    try {
        ado::RecordsetPtr rs=con->Execute(_bstr_t(sql.data()),NULL,ADODB::adOptionUnspecified);
        if(!rs->GetadoEOF())
        {

            for(long i=0;s_kmap[i];i+=2)
            {
                _variant_t vdt=rs->GetCollect(_bstr_t(s_kmap[i]));
                wxVariant wxdt;
                myConvertOleToVariant(vdt,wxdt);
                if(wxdt.IsNull())
                {
                    this->m_brxx[s_kmap[i]]=L"";
                    this->m_brxx[s_kmap[i+1]]=L"";
                }else {
                    std::wstring str=wxdt.GetString().ToStdWstring();
                    this->m_brxx[s_kmap[i]]=str;
                    this->m_brxx[s_kmap[i+1]]=str;
                }

            }

            m_brxx[L"年岁"]=m_brxx[L"年龄"]+m_brxx[L"年龄单位"];

            this->m_tcBRXM->SetValue(this->m_brxx[L"BRXM"]);
            this->m_tcBRXB->SetValue(this->m_brxx[L"BRXB"]);
            this->m_tcBRNL->SetValue(this->m_brxx[L"BRNL"]+this->m_brxx[L"BRNLDW"]);
            this->m_tcZYH->SetValue(this->m_brxx[L"BLH"]);
            this->m_tcDQKS->SetValue(this->m_brxx[L"DQKS"]);
            this->m_tcDQKS->SetClientObject(TClientData<std::wstring>::New(this->m_brxx[L"DQKSDM"]));
            this->m_tcCWYS->SetValue(this->m_brxx[L"CWYS"]);
            this->m_tcCWYS->SetClientObject(TClientData<std::wstring>::New(this->m_brxx[L"CWYSDM"]));
            this->m_tbname=this->m_brxx[L"TBNAME"];
            std::wstring  cyzt=this->m_brxx[L"CYZT"];
            if(this->m_tbname==L"ZYDJ_BRZL")
            {
                if(cyzt.empty())
                {
                    this->m_tcZYZT->SetValue(L"住院");
                }else{

                    this->m_tcZYZT->SetValue(L"出院未结账");

                }
            }else
            {
                this->m_tcZYZT->SetValue(L"出院已结账");
            }
        }

    } catch (_com_error & e) {
        ado::ShowError(e);
    }

}


